Binarycrossentropy ================= 计算二元交叉熵损失。用于衡量二分类问题中预测值 (`input_x`) 和目标值 (`input_y`) 之间的差距。 .. math:: L_n = -w_n [y_n \cdot \log(x_n) + (1 - y_n) \cdot \log(1 - x_n)] 其中 :math:`x_n` 是预测值, :math:`y_n` 是目标值, :math:`w_n` 是可选的样本权重。最终输出由 `reduction` 参数决定: - **None (0):** 不进行规约,输出每个元素的损失 :math:`L_n`。 - **Mean (1):** 输出所有元素损失的平均值。 - **Sum (2):** 输出所有元素损失的总和。 输入: - **input_size** - 输入张量的总元素数量。 - **reduction** - 规约类型 (0: None, 1: Mean, 2: Sum)。 - **input_x** - 预测值的张量数据地址,通常是Sigmoid函数的输出。 - **input_y** - 目标值(标签)的张量数据地址。 - **weight** - (可选) 权重张量的数据地址,维度与 `input_x` 相同。 - **loss** - (输出) 最终损失值的数据地址。如果 `reduction` 为 `None`,其大小为 `input_size`;否则大小为1。 - **tmp_loss** - 用于存储逐元素损失的临时工作空间地址,大小必须为 `input_size`。 - **weight_defined** - 权重是否有效的标志。若为非0,则`weight`参数必须提供。 - **core_mask** - 核掩码。 输出: - **loss** - 写入最终损失值的数据地址。 支持平台: ``FT78NE`` ``MT7004`` .. note:: - FT78NE 支持fp32, int8 - MT7004 支持fp16, fp32 **共享存储版本:** .. c:function:: void fp_binarycrossentropy_s(int input_size, int reduction, float* input_x, float* input_y, float* weight, float* loss, float* tmp_loss, int weight_defined, int core_mask) .. c:function:: void hp_binarycrossentropy_s(int input_size, int reduction, half* input_x, half* input_y, half* weight, half* loss, half* tmp_loss, int weight_defined, int core_mask) .. c:function:: void i8_binarycrossentropy_s(int input_size, int reduction, int8_t* input_x, int8_t* input_y, int8_t* weight, int8_t* loss, int8_t* tmp_loss, int weight_defined, int core_mask) **C调用示例:** .. code-block:: c :linenos: :emphasize-lines: 16 //FT78NE示例 #include #include int main(int argc, char* argv[]) { float *input_x = (float *)0xA0000000; // input_x 在DDR空间 float *input_y = (float *)0xB0000000; // input_y float *weight = (float *)0xC0000000; // weight float *loss = (float *)0xD0000000; // loss output float *tmp_loss = (float *)0xE0000000; // temp workspace int input_size = 1024; int reduction = 1; // Mean int weight_defined = 1; // true int core_mask = 0xff; fp_binarycrossentropy_s(input_size, reduction, input_x, input_y, weight, loss, tmp_loss, weight_defined, core_mask); return 0; } **私有存储版本:** .. c:function:: void fp_binarycrossentropy_p(int input_size, int reduction, float* input_x, float* input_y, float* weight, float* loss, float* tmp_loss, int weight_defined) .. c:function:: void hp_binarycrossentropy_p(int input_size, int reduction, half* input_x, half* input_y, half* weight, half* loss, half* tmp_loss, int weight_defined) .. c:function:: void i8_binarycrossentropy_p(int input_size, int reduction, int8_t* input_x, int8_t* input_y, int8_t* weight, int8_t* loss, int8_t* tmp_loss, int weight_defined) **C调用示例:** .. code-block:: c :linenos: :emphasize-lines: 15 //FT78NE示例 #include #include int main(int argc, char* argv[]) { float *input_x = (float *)0x10000000; // input_x 在L2空间 float *input_y = (float *)0x11000000; // input_y float *weight = (float *)0x12000000; // weight float *loss = (float *)0x13000000; // loss output float *tmp_loss = (float *)0x14000000; // temp workspace int input_size = 1024; int reduction = 1; // Mean int weight_defined = 0; // false fp_binarycrossentropy_p(input_size, reduction, input_x, input_y, weight, loss, tmp_loss, weight_defined); return 0; }